# -*- coding: utf-8 -*- 
"""
Project: automation-api-project
Creator: jinsheng
Create time: 2021-11-26 09:03
IDE: PyCharm
Introduction: ods_device_electric_ts表造数据
"""
from datetime import timedelta
from multiprocessing import Process
from threading import Thread
import random
from datetime import datetime
from common.hbaseDataOperate import HbaseOperate

# hb = HbaseOperate(table_name='ods_device_electric_ts')
# print(hb.query_data())
# print(hb.query_data(row_prefix=b'00000010111202102727498283884544'))
# tm = datetime(2021, 11, 1, 0, 0)
# fmt = '%Y%m%d%H%M'
# fmt_s = '%Y%m%d%H%M%S'
# lis = [i for i in range(10000000) if i % 100000 == 0]
# lis = [i for i in range(10000000) if i % 50000 == 0]
# hb = HbaseOperate(table_name='ods_device_electric_ts')
lis = [i for i in range(0, 1000) if i % 100 == 0]


def insert(start=None):
    hb = HbaseOperate(table_name='ods_device_electric_ts')
    fmt_s = '%Y%m%d%H%M%S'
    for j in range(start + 1, start + 3):
        # for j in range(96):
        # device_id = str(100000000000000000 + j)
        device_id = str(10000000 + j)
        # device_id = '121926203731877888'
        tm = datetime(2019, 10, 1, 0, 0)
        efaa_actual_kwh = 2000.00  # A相正向有功电量实际值(千瓦时)
        efab_actual_kwh = 1000.00  # B相正向有功电量实际值(千瓦时)
        efac_actual_kwh = 500.00  # C相正向有功电量实际值(千瓦时)
        # efat_actual_kwh = efaa_actual_kwh + efab_actual_kwh + efac_actual_kwh  # 正向有功总电量实际值(千瓦时)
        efrt_actual_kvarh = 200.00  # 正向无功总电量实际值(千乏时)
        eraa_actual_kwh = 600.00  # A相反向有功电量实际值(千瓦时)
        erab_actual_kwh = 300.00  # B相反向有功电量实际值(千瓦时)
        erac_actual_kwh = 150.00  # C相反向有功电量实际值(千瓦时)
        # erat_actual_kwh = eraa_actual_kwh + erab_actual_kwh + erac_actual_kwh  # 反向有功总电量实际值(千瓦时)
        errt_actual_kvarh = 40.00  # 反向无功总电量实际值(千乏时)
        ds_origin_kw = 200.0  # 正向有功尖需量原始值(千瓦)
        dp_origin_kw = 200.0  # 正向有功峰需量原始值(千瓦)
        df_origin_kw = 200.0  # 正向有功平需量原始值(千瓦)
        do_origin_kw = 200.0  # 正向有功谷需量原始值(千瓦)
        # dt_origin_kw = ds_origin_kw + dp_origin_kw + df_origin_kw + do_origin_kw  # 正向有功需量原始值(千瓦)
        indfaa_origin_kwh = 2000.0  # A相正向有功示值原始值(千瓦时)
        indfab_origin_kwh = 1000.0  # B相正向有功示值原始值(千瓦时)
        indfac_origin_kwh = 500.0  # C相正向有功示值原始值(千瓦时)
        # indfat_origin_kwh = indfaa_origin_kwh + indfab_origin_kwh + indfac_origin_kwh  # 正向有功总示值原始值(千瓦时)
        indfrt_origin_kvarh = 1000.0  # 正向无功总示值原始值(千乏时)
        indraa_origin_kwh = 400.0  # A相反向有功示值原始值(千瓦时)
        indrab_origin_kwh = 200.0  # B相反向有功示值原始值(千瓦时)
        indrac_origin_kwh = 100.0  # C相反向有功示值原始值(千瓦时)
        # indrat_origin_kwh = indraa_origin_kwh + indrab_origin_kwh + indrac_origin_kwh  # 反向有功总示值原始值(千瓦时)
        indrp1_origin_kvarh = 100.0  # 一象限无功示值原始值(千乏时)
        indrp2_origin_kvarh = 80.0  # 二象限无功示值原始值(千乏时)
        indrp3_origin_kvarh = 60.0  # 三象限无功示值原始值(千乏时)
        indrp4_origin_kvarh = 40.0  # 四象限无功示值原始值(千乏时)
        # indrrt_origin_kvarh = indrp1_origin_kvarh + indrp2_origin_kvarh + indrp3_origin_kvarh + indrp4_origin_kvarh  # 反向无功总示值原始值(千乏时)
        dt_time_empty = str(tm.strftime(fmt_s))  # 正向有功需量发生时间(无单位)
        ds_time_empty = str(tm.strftime(fmt_s))  # 正向有功尖需量发生时间(无单位)
        dp_time_empty = str(tm.strftime(fmt_s))  # 正向有功峰需量发生时间(无单位)
        df_time_empty = str(tm.strftime(fmt_s))  # 正向有功平需量发生时间(无单位)
        do_time_empty = str(tm.strftime(fmt_s))  # 正向有功谷需量发生时间(无单位)
        for i in range(96):
            tm_res = str(tm.strftime(fmt_s))[::-1]
            data_source = random.randint(1, 2)
            row_key = tm_res + device_id + 'mi15'
            increase_num = round(random.uniform(0.0, 4.0), 2)
            ua_origin_v = round(random.uniform(210.0, 230.0), 2)  # A相电压原始值(伏特)
            ub_origin_v = round(random.uniform(210.0, 230.0), 2)  # B相电压原始值(伏特)
            uc_origin_v = round(random.uniform(210.0, 230.0), 2)  # C相电压原始值(伏特)
            ia_origin_a = round(random.uniform(3.0, 4.0), 2)  # A相电流原始值(安培)
            ib_origin_a = round(random.uniform(3.0, 4.0), 2)  # B相电流原始值(安培)
            ic_origin_a = round(random.uniform(3.0, 4.0), 2)  # C相电流原始值(安培)
            pa_actual_kw = round((ua_origin_v * ia_origin_a), 2)  # A相有功功率实际值(千瓦)
            pb_actual_kw = round((ub_origin_v * ib_origin_a), 2)  # B相有功功率实际值(千瓦)
            pc_actual_kw = round((uc_origin_v * ic_origin_a), 2)  # C相有功功率实际值(千瓦)
            pt_actual_kw = pa_actual_kw + pb_actual_kw + pc_actual_kw  # 总有功功率实际值(千瓦)
            qa_actual_kvar = round(random.uniform(1.0, 2.0), 2)  # A相无功功率实际值(千乏)
            qb_actual_kvar = round(random.uniform(1.0, 2.0), 2)  # B相无功功率实际值(千乏)
            qc_actual_kvar = round(random.uniform(1.0, 2.0), 2)  # C相无功功率实际值(千乏)
            pfa_origin_empty = round(random.uniform(0, 1.0), 2)  # A相功率因数原始值(无单位)
            pfb_origin_empty = round(random.uniform(0, 1.0), 2)  # B相功率因数原始值(无单位)
            pfc_origin_empty = round(random.uniform(0, 1.0), 2)  # C相功率因数原始值(无单位)
            pft_origin_empty = pfa_origin_empty + pfb_origin_empty + pfc_origin_empty  # 总功率因数原始值(无单位)
            qt_actual_kvar = qa_actual_kvar + qb_actual_kvar + qc_actual_kvar  # 总无功功率实际值(千乏)
            iz_origin_a = round(random.uniform(3.0, 4.0), 2)  # 零相电流原始值(安培)
            uaa_origin_d = round(random.uniform(10.0, 30.0), 2)  # A相电压相位角原始值(度)
            uab_origin_d = round(random.uniform(10.0, 30.0), 2)  # B相电压相位角原始值(度)
            uac_origin_d = round(random.uniform(10.0, 30.0), 2)  # C相电压相位角原始值(度)
            iaa_origin_d = round(random.uniform(5.0, 10.0), 2)  # A相电流相位角原始值(度)
            iab_origin_d = round(random.uniform(5.0, 10.0), 2)  # B相电流相位角原始值(度)
            iac_origin_d = round(random.uniform(5.0, 10.0), 2)  # C相电流相位角原始值(度)
            ta_origin_dc = round(random.uniform(20.0, 40.0), 2)  # A相温度原始值(摄氏度)
            tb_origin_dc = round(random.uniform(20.0, 40.0), 2)  # B相温度原始值(摄氏度)
            tc_origin_dc = round(random.uniform(20.0, 40.0), 2)  # C相温度原始值(摄氏度)
            paa_origin_kw = pa_actual_kw  # A相分钟平均有功功率原始值(千瓦)
            pab_origin_kw = pb_actual_kw  # B相分钟平均有功功率原始值(千瓦)
            pac_origin_kw = pc_actual_kw  # C相分钟平均有功功率原始值(千瓦)
            iub_origin_d = round(random.uniform(2.0, 5.0), 2)  # 电流不平衡度原始值(度)
            uub_origin_d = round(random.uniform(2.0, 5.0), 2)  # 电压不平衡度原始值(度)
            indrat_origin_kwh = indraa_origin_kwh + indrab_origin_kwh + indrac_origin_kwh  # 反向有功总示值原始值(千瓦时)
            indrrt_origin_kvarh = indrp1_origin_kvarh + indrp2_origin_kvarh + indrp3_origin_kvarh + indrp4_origin_kvarh  # 反向无功总示值原始值(千乏时)
            indfat_origin_kwh = indfaa_origin_kwh + indfab_origin_kwh + indfac_origin_kwh  # 正向有功总示值原始值(千瓦时)
            dt_origin_kw = ds_origin_kw + dp_origin_kw + df_origin_kw + do_origin_kw  # 正向有功需量原始值(千瓦)
            erat_actual_kwh = eraa_actual_kwh + erab_actual_kwh + erac_actual_kwh  # 反向有功总电量实际值(千瓦时)
            efat_actual_kwh = efaa_actual_kwh + efab_actual_kwh + efac_actual_kwh  # 正向有功总电量实际值(千瓦时)
            data = {'cf:device_id': device_id, 'cf:data_time': str(tm.strftime(fmt_s)),
                    'cf:efaa_actual_kwh': str(efaa_actual_kwh),
                    'cf:efab_actual_kwh': str(efab_actual_kwh),
                    'cf:efac_actual_kwh': str(efac_actual_kwh),
                    'cf:efat_actual_kwh': str(efat_actual_kwh),
                    'cf:efrt_actual_kvarh': str(efrt_actual_kvarh),
                    'cf:eraa_actual_kwh': str(eraa_actual_kwh),
                    'cf:erab_actual_kwh': str(erab_actual_kwh),
                    'cf:erac_actual_kwh': str(erac_actual_kwh),
                    'cf:erat_actual_kwh': str(erat_actual_kwh),
                    'cf:errt_actual_kvarh': str(errt_actual_kvarh),
                    'cf:ds_origin_kw': str(ds_origin_kw),
                    'cf:dp_origin_kw': str(dp_origin_kw),
                    'cf:df_origin_kw': str(df_origin_kw),
                    'cf:do_origin_kw': str(do_origin_kw),
                    'cf:dt_origin_kw': str(dt_origin_kw),
                    'cf:indfaa_origin_kwh': str(indfaa_origin_kwh),
                    'cf:indfab_origin_kwh': str(indfab_origin_kwh),
                    'cf:indfac_origin_kwh': str(indfac_origin_kwh),
                    'cf:indfat_origin_kwh': str(indfat_origin_kwh),
                    'cf:indfrt_origin_kvarh': str(indfrt_origin_kvarh),
                    'cf:indraa_origin_kwh': str(indraa_origin_kwh),
                    'cf:indrab_origin_kwh': str(indrab_origin_kwh),
                    'cf:indrac_origin_kwh': str(indrac_origin_kwh),
                    'cf:indrat_origin_kwh': str(indrat_origin_kwh),
                    'cf:indrp1_origin_kvarh': str(indrp1_origin_kvarh),
                    'cf:indrp2_origin_kvarh': str(indrp2_origin_kvarh),
                    'cf:indrp3_origin_kvarh': str(indrp3_origin_kvarh),
                    'cf:indrp4_origin_kvarh': str(indrp4_origin_kvarh),
                    'cf:indrrt_origin_kvarh': str(indrrt_origin_kvarh),
                    'cf:dt_time_empty': str(dt_time_empty),
                    'cf:ds_time_empty': str(ds_time_empty),
                    'cf:dp_time_empty': str(dp_time_empty),
                    'cf:df_time_empty': str(df_time_empty),
                    'cf:do_time_empty': str(do_time_empty),
                    'cf:ua_origin_v': str(ua_origin_v),
                    'cf:ub_origin_v': str(ub_origin_v),
                    'cf:uc_origin_v': str(uc_origin_v),
                    'cf:ia_origin_a': str(ia_origin_a),
                    'cf:ib_origin_a': str(ib_origin_a),
                    'cf:ic_origin_a': str(ic_origin_a),
                    'cf:pa_actual_kw': str(pa_actual_kw),
                    'cf:pb_actual_kw': str(pb_actual_kw),
                    'cf:pc_actual_kw': str(pc_actual_kw),
                    'cf:pt_actual_kw': str(pt_actual_kw),
                    'cf:qa_actual_kvar': str(qa_actual_kvar),
                    'cf:qb_actual_kvar': str(qb_actual_kvar),
                    'cf:qc_actual_kvar': str(qc_actual_kvar),
                    'cf:pfa_origin_empty': str(pfa_origin_empty),
                    'cf:pfb_origin_empty': str(pfb_origin_empty),
                    'cf:pfc_origin_empty': str(pfc_origin_empty),
                    'cf:pft_origin_empty': str(pft_origin_empty),
                    'cf:qt_actual_kvar': str(qt_actual_kvar),
                    'cf:iz_origin_a': str(iz_origin_a),
                    'cf:uaa_origin_d': str(uaa_origin_d),
                    'cf:uab_origin_d': str(uab_origin_d),
                    'cf:uac_origin_d': str(uac_origin_d),
                    'cf:iaa_origin_d': str(iaa_origin_d),
                    'cf:iab_origin_d': str(iab_origin_d),
                    'cf:iac_origin_d': str(iac_origin_d),
                    'cf:ta_origin_dc': str(ta_origin_dc),
                    'cf:tb_origin_dc': str(tb_origin_dc),
                    'cf:tc_origin_dc': str(tc_origin_dc),
                    'cf:paa_origin_kw': str(paa_origin_kw),
                    'cf:pab_origin_kw': str(pab_origin_kw),
                    'cf:pac_origin_kw': str(pac_origin_kw),
                    'cf:iub_origin_d': str(iub_origin_d),
                    'cf:uub_origin_d': str(uub_origin_d),
                    'cf:data_source': str(data_source), 'cf:frequency': 'mi15'}
            tm += timedelta(minutes=15)
            hb.insert_data(row_key, data)
            efaa_actual_kwh += increase_num
            efab_actual_kwh += increase_num
            efac_actual_kwh += increase_num
            efrt_actual_kvarh += increase_num
            eraa_actual_kwh += increase_num
            erab_actual_kwh += increase_num
            erac_actual_kwh += increase_num
            errt_actual_kvarh += increase_num
            ds_origin_kw += increase_num
            dp_origin_kw += increase_num
            df_origin_kw += increase_num
            do_origin_kw += increase_num
            indfaa_origin_kwh += increase_num
            indfab_origin_kwh += increase_num
            indfac_origin_kwh += increase_num
            indfrt_origin_kvarh += increase_num
            indraa_origin_kwh += increase_num
            indrab_origin_kwh += increase_num
            indrac_origin_kwh += increase_num
            indrp1_origin_kvarh += increase_num
            indrp2_origin_kvarh += increase_num
            indrp3_origin_kvarh += increase_num
            indrp4_origin_kvarh += increase_num


def insert_one(start=None):
    hb = HbaseOperate(table_name='ods_device_electric_ts')
    fmt_s = '%Y%m%d%H%M%S'
    tm = datetime(2021, 11, 1, 0, 0)
    # device_id = str(100000000000000000 + start)
    device_id = '121926203731877888'
    indfat_origin_kwh = 2000.0
    indfrt_origin_kvarh = 500.0
    indrat_origin_kwh = 200.0
    indrrt_origin_kvarh = 50.0
    for i in range(1):
        tm_res = str(tm.strftime(fmt_s))[::-1]
        ua_origin_v = round(random.uniform(215.0, 225.0), 2)
        ub_origin_v = round(random.uniform(215.0, 225.0), 2)
        data_source = random.randint(1, 2)
        random_increase = round(random.uniform(10.0, 20.0), 2)
        row_key = tm_res + device_id + 'mi15'
        data = {'cf:device_id': device_id, 'cf:data_time': str(tm.strftime(fmt_s)),
                'cf:ua_origin_v': str(ua_origin_v), 'cf:ub_origin_v': str(ub_origin_v),
                'cf:indfat_origin_kwh': str(indfat_origin_kwh + random_increase),
                'cf:indfrt_origin_kvarh': str(indfrt_origin_kvarh + random_increase),
                'cf:indrat_origin_kwh': str(indrat_origin_kwh + random_increase),
                'cf:indrrt_origin_kvarh': str(indrrt_origin_kvarh + random_increase),
                'cf:data_source': str(data_source), 'cf:frequency': 'mi15'}
        tm += timedelta(minutes=15)
        hb.insert_data(row_key, data)
        indfat_origin_kwh += random_increase
        indfrt_origin_kvarh += random_increase
        indrat_origin_kwh += random_increase
        indrrt_origin_kvarh += random_increase


if __name__ == '__main__':
    # process_list = []
    # for i in lis:  # 开启5个子进程执行fun1函数
    #     p = Process(target=insert, args=(i,))  # 实例化进程对象
    #     p.start()
    #     process_list.append(p)
    #
    # for i in process_list:
    #     i.join()
    # hb = HbaseOperate(table_name='ods_device_electric_ts')
    # hb.query_data()
    # insert(0)

    insert_one()